package cn.etgps.etgps.blmanager;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Base64;

import cn.etgps.etgps.R;
import cn.etgps.etgps.manager.DatabaseManager;
import cn.etgps.etgps.utils.AppUtils;
import cn.etgps.etgps.utils.Constant;


/**
 * all business layer manager's parent
 *
 * @author nicky.han
 */
public class BaseBLManager {
    /**
     * Count about load items per page
     */
    public static final int DEFAULT_PER_PAGE_COUNT = 20;
    /**
     * Use to operate the local DB
     */
    private static DatabaseManager mDBManager = null;
    /**
     * Used to get UI resource
     */
    public Context mContext = null;

    /**
     * Initialize adapter
     */
    public BaseBLManager(Context context) {
        mContext = context;
    }

    /**
     * used to check the network is available or not
     */
    public static boolean checkNetwork(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 在保存密码前进行加密
     *
     * @param realPassword 未加密的密码
     * @return 加密后的密码
     */
    public static String encriptPassword(String realPassword) {
        return Base64.encodeToString(realPassword.getBytes(), Base64.DEFAULT);
    }

    /**
     * 解密密码
     *
     * @param encriptPassword 加密后的密码
     * @return 解密后密码
     */
    public static String decriptPassword(String encriptPassword) {
        return new String(Base64.decode(encriptPassword.getBytes(), Base64.DEFAULT));
    }

    /**
     * Open the DatabaseManager before operate DB
     */
    public DatabaseManager getDBManager() {
        if (mDBManager == null) {
            mDBManager = new DatabaseManager(mContext);
        }
        return mDBManager;
    }

    /**
     * close the DatabaseManager when it is useless
     */
    public void closeDBManager() {
        if (null != mDBManager) {
            mDBManager.closeManager();
            mDBManager = null;
        }
    }

    /**
     * Show toast using the message and duration.
     *
     * @param message  the message shown in toast.
     * @param duration the time for which the toast will be shown.
     */
    public void showToastMessage(String message, int duration) {
        AppUtils.showToastMessage(mContext, message, duration);
    }

    /**
     * 保存用户基本信息
     *
     * @param info 用户信息的数组
     * @return 保存结果
     */
    public boolean saveUserLoginBasicInfo(String[] info) {
        SharedPreferences mPerference = mContext.getSharedPreferences(
                mContext.getResources().getString(R.string.preference_key), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = mPerference.edit();
        editor.putString(Constant.USER_NAME, info[0]);
        editor.putString(Constant.USER_ID, info[1]);
        editor.putString(Constant.USER_TYPE, info[2]);
        editor.putString(Constant.USER_PASSWORD, encriptPassword(info[3]));
        editor.putString(Constant.REMEMBER_PASSWORD, info[4]);
        return editor.commit();
    }

    /**
     * 从本地保存中获取用户基本信息
     *
     * @return 用户信息数组
     */
    public String[] getUserInfo() {
        String[] loginInfo = new String[3];
        SharedPreferences mPerference = mContext.getSharedPreferences(
                mContext.getResources().getString(R.string.preference_key), Context.MODE_PRIVATE);
        loginInfo[0] = mPerference.getString(Constant.USER_NAME, "");
        loginInfo[1] = decriptPassword(mPerference.getString(Constant.USER_PASSWORD, ""));
        loginInfo[2] = mPerference.getString(Constant.REMEMBER_PASSWORD, "");
        return loginInfo;
    }

    /**
     * 获取用户类型
     */
    public String getUserType() {
        SharedPreferences mPerference = mContext.getSharedPreferences(
                mContext.getResources().getString(R.string.preference_key), Context.MODE_PRIVATE);
        return mPerference.getString(Constant.USER_TYPE, "");
    }

    /**
     * 获取用户ID
     */
    public String getUserID() {
        SharedPreferences mPerference = mContext.getSharedPreferences(
                mContext.getResources().getString(R.string.preference_key), Context.MODE_PRIVATE);
        return mPerference.getString(Constant.USER_ID, "");
    }
}
